package com.ujs.edu.medicine.dao;

import com.ujs.edu.medicine.dataobject.YunjiayiComment;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface YunjiayiCommentMapper {
    @Delete({
        "delete from yunjiayi_comment",
        "where comment_id = #{commentId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer commentId);

    @Insert({
        "insert into yunjiayi_comment (comment_id, create_time, ",
        "delete_time, content, ",
        "author_id, parentId, ",
        "post_Id)",
        "values (#{commentId,jdbcType=INTEGER}, now(), ",
        "now(), #{content,jdbcType=VARCHAR}, ",
        "#{authorId,jdbcType=INTEGER}, #{parentid,jdbcType=INTEGER}, ",
        "#{postId,jdbcType=INTEGER})"
    })
    int insert(YunjiayiComment record);

    @Select({
        "select",
        "comment_id, create_time, delete_time, content, author_id, parentId, post_Id",
        "from yunjiayi_comment",
        "where comment_id = #{commentId,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="comment_id", property="commentId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="author_id", property="authorId", jdbcType=JdbcType.INTEGER),
        @Result(column="parentId", property="parentid", jdbcType=JdbcType.INTEGER),
        @Result(column="post_Id", property="postId", jdbcType=JdbcType.INTEGER)
    })
    YunjiayiComment selectByPrimaryKey(Integer commentId);

    @Select({
        "select",
        "comment_id, create_time, delete_time, content, author_id, parentId, post_Id",
        "from yunjiayi_comment"
    })
    @Results(id = "commentMap",value = {
        @Result(column="comment_id", property="commentId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
        @Result(column="author_id", property="authorId", jdbcType=JdbcType.INTEGER),
        @Result(column="parentId", property="parentid", jdbcType=JdbcType.INTEGER),
        @Result(column="post_Id", property="postId", jdbcType=JdbcType.INTEGER)
    })
    List<YunjiayiComment> selectAll();


    /*通过postId查询*/
    @Select("select * from yunjiayi_comment where post_Id=#{postId}")
    @ResultMap("commentMap")
    List<YunjiayiComment> getCommentsByPostId(@Param("postId")Integer postId);


    /*通过帖子ID统计帖子的评论数量*/
    @Select("select count(*) from yunjiayi_comment where post_Id=#{postId}")
    Integer countCommenyNum(@Param("postId")Integer postId);

    @Update({
        "update yunjiayi_comment",
        "set create_time = #{createTime,jdbcType=TIMESTAMP},",
          "delete_time = #{deleteTime,jdbcType=TIMESTAMP},",
          "content = #{content,jdbcType=VARCHAR},",
          "author_id = #{authorId,jdbcType=INTEGER},",
          "parentId = #{parentid,jdbcType=INTEGER},",
          "post_Id = #{postId,jdbcType=INTEGER}",
        "where comment_id = #{commentId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(YunjiayiComment record);
}